//
// Created by 29491 on 2025/2/2.
//

#include "Flash_save.h"
#include "main.h"
//#include "usart.h"
#include "gpio.h"
uint8_t data_xianzaide[DATA_SIZE];             // ??????

uint8_t Data[DATA_SIZE] = {1,2,3,4,5};      // ??????


// Flash????
void FLASH_WriteData(uint32_t address, uint8_t *data, uint16_t length)
{

    FLASH_EraseInitTypeDef eraseInitStruct;
    uint32_t sectorError;

    eraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
    eraseInitStruct.Sector = Get_Sector(FLASH_USER_START_ADDR);  // ????6
    eraseInitStruct.NbSectors = 1;            // ??1???
    eraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;

    if (HAL_FLASHEx_Erase(&eraseInitStruct, &sectorError) != HAL_OK)
	{
        // ????
        HAL_FLASH_Lock();  // ??Flash
        return;
    }
	
//    uint8_t erase_verify[DATA_SIZE];
//    FLASH_ReadData(FLASH_USER_START_ADDR, erase_verify, DATA_SIZE);
//    for (int i = 0; i < DATA_SIZE; i++)
//	{
//        if (erase_verify[i] != 0xFF)
//		{
//			HAL_FLASH_Lock();  // 锁定 Flash
//            break;
//        }
//    }
	
    for (uint16_t i = 0; i < length; i++)
    {
        if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_BYTE, address + i, data[i]) != HAL_OK) {
            HAL_FLASH_Lock();  // ??Flash
            return;
        }
    }
	
//    uint8_t write_verify[DATA_SIZE];
//	FLASH_ReadData(FLASH_USER_START_ADDR, write_verify, DATA_SIZE);
//    if (memcmp(write_verify, data, DATA_SIZE) == 0) {
////        HAL_UART_Transmit(&huart1, (const uint8_t *)"Write_Success\n", 14, 100);
//    } else {
////        HAL_UART_Transmit(&huart1, (const uint8_t *)"Write_Fail\n", 11, 100);
//    }
	
	
    HAL_FLASH_Lock();
}

//Flash????
void FLASH_ReadData(uint32_t address, uint8_t *data, uint16_t length)
{
    for (uint16_t i = 0; i < length; i++)
	{
        data[i] = *(__IO uint8_t*)(address + i);  // ????
    }
}


void Flash_Init(void)
{
    FLASH_ReadData(FLASH_USER_START_ADDR,Data,DATA_SIZE);
    while(1)
    {
        FLASH_ReadData(FLASH_USER_START_ADDR,data_xianzaide,DATA_SIZE);
        HAL_Delay(1000);
        Data[0] += 1;
        Data[1] += 2;
        Data[2] += 3;
        Data[3] += 4;
        Data[4] += 5;
    }
}

//void Flash_Test(void)
//{
//	
//}



//void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin)
//{
//    HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_13);
//    // ??Flash
//    FLASH_WriteData(FLASH_USER_START_ADDR, Data, DATA_SIZE);
//}


uint32_t Get_Sector(uint32_t address)
{
    if (address < 0x08004000) return FLASH_SECTOR_0;
    else if (address < 0x08008000) return FLASH_SECTOR_1;
    else if (address < 0x0800C000) return FLASH_SECTOR_2;
    else if (address < 0x08010000) return FLASH_SECTOR_3;
    else if (address < 0x08020000) return FLASH_SECTOR_4;
    else if (address < 0x08040000) return FLASH_SECTOR_5;
    else if (address < 0x08060000) return FLASH_SECTOR_6;
    else if (address < 0x08080000) return FLASH_SECTOR_7;
    else if (address < 0x080A0000) return FLASH_SECTOR_8;
    else if (address < 0x080C0000) return FLASH_SECTOR_9;
    else if (address < 0x080E0000) return FLASH_SECTOR_10;
    else if (address < 0x08100000) return FLASH_SECTOR_11;
    else return FLASH_SECTOR_11;  // 默认返回最后一个扇区
}



